mybatis 您所在的位置:网站首页 mybatis 小于怎么写 mybatis

mybatis

2024-06-26 19:28| 来源: 网络整理| 查看: 265

点滴记载,点滴进步,愿自己更上一层楼。

用mybatis执行数据库操作仅仅能看到执行结果,如果想看到执行的sql语句怎么办。

查阅mybatis官方文档找到了解决方法。

配置什么的很简单,用的log4j打印,当然参照官方文档还有好几种方法,具体自弄。

这里仅作记录只用。配置很简单,将log4j架包加入到classpath里。

maven配置。

log4j log4j 1.2.17

 

非maven项目只需要将jar添加到项目中即可。

log4j.properties添加到source根目录。

# Global logging configuration log4j.rootLogger=ERROR, stdout # MyBatis logging configuration... #log4j.logger.com.soft.test.dao=DEBUG log4j.logger.dynamic=DEBUG #log4j.logger.org.mybatis.example.BlogMapper=TRACE # Console output... log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

 

其中关键的地方是log4j.logger.dynamic=DEBUG

log4j.logger是固定的,dynamic为你的mapper.xml的namespace

如果我的xml中的namespace为dynamic

DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> select * from t_user where id=#{id} and username like concat('%',#{username},'%') and password=#{password}

配置完成。现在运行测试即可看到运行的sql语句------------------------------------------------------------------------------------------------------------------------------

DEBUG [main] - ==>  Preparing: select * from t_user where id=? and username like concat('%',?,'%') DEBUG [main] - ==> Parameters: 28(Integer), xiao(String)DEBUG [main] -

 mybatis对于这种大于小于等等还有另一种形式。

例如:

对应关系:

---------------------------------------

    gt            对应             >

    gte         对应              >=

    lt             对应                   

2.4 是否是某个特定字符串,某些业务有此需要。

例如: 或者

注意:

这种形式的写法在参数类型是字符串的时候是没有问题的,

但是参数类型为非字符串类型的时候就需要写成 

仅仅写成也会有很大可能会挂。

也许你会说非字符串的为什么要写成这样。这就要看特俗需要了。

例如:某一个sql片段是公用的,

该片段更新条件也用,但是当你需要将某一个字段更新成null的时候怎么办。

这个时候就可以通过传入一个特定的字符串来弄。当传入的字符串为特定字符串的时候就更新该字符串为null。

xxx=null

当然这样子貌似date型会挂。

通过 2.2 也可以看出mybatis对于字符串的相等不相等的判断也是有对应的特俗操作符的。

-------------------------------------------------------

eq                  对应                ==

neq               对应                 !=

------------------------------------------------------

当然还可以看出来if的条件判断test是支持对象自身方法调用的,即使是自己写的方法,可以自己尝试。当然下面会有例子。

例如:里面可以用‘xxxx’.equals(xxxx) 字符串的比较两个字符串方法

    xxxx.indexOf('ss') 判断字符串里面是否包含某个字符等等  

 3 判断list是否为空

上面说过,if条件判断可以直接调用对象自身的方法进行逻辑判断,所以list判空。可以调用.size()>0或者.isEmpty()

例如: , 

4 map参数同同理  取值的话 map.key(map中的key名字)即可

----------------------------------------------------------------------------------------------分割线01-----------------------------------------------------------------------------------------------------------

这里是上面的各种理论的实践。这里可以不看,自己去实践最好。

1 数字类型。

仅作null判断。

select * from t_user where 1=1 and id=#{id}

当id不为null的时候 打印的logDEBUG [main] - ==>  Preparing: select * from t_user where 1=1 and id=? DEBUG [main] - ==> Parameters: 28(Integer)DEBUG [main] -  Preparing: select * from t_user where 1=1 DEBUG [main] - ==> Parameters: DEBUG [main] -

当传入id=28的时候DEBUG [main] - ==>  Preparing: select * from t_user where 1=1 and id=? DEBUG [main] - ==> Parameters: 28(Integer)DEBUG [main] -  Preparing: select * from t_user where 1=1 DEBUG [main] - ==> Parameters: DEBUG [main] -

    gte         对应              >=

    lt             对应               Parameters:

参数  id=28DEBUG [main] - ==>  Preparing: select * from t_user where 1=1 and id=? DEBUG [main] - ==> Parameters: 28(Integer)

 >= 

参数  id=28DEBUG [main] - ==>  Preparing: select * from t_user where 1=1 and id=? DEBUG [main] - ==> Parameters: 28(Integer)

参数  id=27

DEBUG [main] - ==>  Preparing: select * from t_user where 1=1 DEBUG [main] - ==> Parameters: 

gte   

参数  id=28DEBUG [main] - ==>  Preparing: select * from t_user where 1=1 and id=? DEBUG [main] - ==> Parameters: 28(Integer)

参数  id=27

DEBUG [main] - ==>  Preparing: select * from t_user where 1=1 DEBUG [main] - ==> Parameters: 

    使用 <    Preparing: select * from t_user where 1=1 DEBUG [main] - ==> Parameters: 参数  id=27

DEBUG [main] - ==>  Preparing: select * from t_user where 1=1 and id=? DEBUG [main] - ==> Parameters: 27(Integer)

lte

参数  id=28  

DEBUG [main] - ==>  Preparing: select * from t_user where 1=1 and id=? DEBUG [main] - ==> Parameters: 28(Integer)

参数  id=29

DEBUG [main] - ==>  Preparing: select * from t_user where 1=1 DEBUG [main] - ==> Parameters: 

----------------------------------------------------------------------------------------------分割线02-----------------------------------------------------------------------------------------------------------

2.1 跟1的第一条一样不做重复测试

2.2 过滤空串

select * from t_user where 1=1 and username=#{username}

!=username=“xiao”DEBUG [main] - ==>  Preparing: select * from t_user where 1=1 and username=? DEBUG [main] - ==> Parameters: xiao(String)

username=“”

DEBUG [main] - ==>  Preparing: select * from t_user where 1=1 DEBUG [main] - ==> Parameters: 

neq

username=“xiao”

DEBUG [main] - ==>  Preparing: select * from t_user where 1=1 and username=? DEBUG [main] - ==> Parameters: xiao(String)

username=“”

DEBUG [main] - ==>  Preparing: select * from t_user where 1=1 DEBUG [main] - ==> Parameters: 

各个逻辑与或的判断and上面已经弄过了,这里弄or  || 两种条件  

or

username=“xiao”

DEBUG [main] - ==>  Preparing: select * from t_user where 1=1 and username=? DEBUG [main] - ==> Parameters: xiao(String)username=“xiaohong”DEBUG [main] - ==>  Preparing: select * from t_user where 1=1 and username=? DEBUG [main] - ==> Parameters: xiaohong(String)

username=“xiaofang”

DEBUG [main] - ==>  Preparing: select * from t_user where 1=1 DEBUG [main] - ==> Parameters: 

||

username=“xiao”

DEBUG [main] - ==>  Preparing: select * from t_user where 1=1 and username=? DEBUG [main] - ==> Parameters: xiao(String)

username=“xiaohong”

DEBUG [main] - ==>  Preparing: select * from t_user where 1=1 and username=? DEBUG [main] - ==> Parameters: xiaohong(String)

username=“xiaofang”

DEBUG [main] - ==>  Preparing: select * from t_user where 1=1 DEBUG [main] - ==> Parameters: 

2.3  indexOf()  lastIndexOf()  判断是否包含某个特定字符

username=“xiao”

DEBUG [main] - ==>  Preparing: select * from t_user where 1=1 and username=? DEBUG [main] - ==> Parameters: xiao(String)

username=“xiaofang”

DEBUG [main] - ==>  Preparing: select * from t_user where 1=1 and username=? DEBUG [main] - ==> Parameters: xiaofang(String)其他两个没什么大不同的。自行测试。2.4   判断是否是某个字符

username=“xiaohong”==>  Preparing: select * from t_user where 1=1 and username=? ==> Parameters: xiaohong(String)

username=“xiaofang”

==>  Preparing: select * from t_user where 1=1 ==> Parameters: 

3  4  的本质就是再说mybatis的if条件判断语句可以直接执行对象的方法。下面自己写一个方法,在if里面试试。

自己定义一个类,里面一个方法用于条件判断用。

public class DynamicSql1Model { public boolean getMySelfMethod(){ return true; } }

该类作为一个属性放入到model中  仅仅贴出部分代码

public class DynamicSqlModel { private int id; private String username; private String password; private Date createDate; private List list; private Map mapParam; private DynamicSql1Model dynamicSql1Model;

xml中引用该model的方法

开始测试

DynamicSqlModel user = new DynamicSqlModel(); user.setUsername("xiaofang"); user.setPassword("123456"); user.setCreateDate(new Date()); DynamicSql1Model dynamicSqlModel = new DynamicSql1Model(); user.setDynamicSql1Model(dynamicSqlModel); dao.selectUseIf(user);

现在返回结果现在方法的返回值为true ==>  Preparing: select * from t_user where 1=1 and username=? ==> Parameters: xiaofang(String)方法返回值修改为false

==>  Preparing: select * from t_user where 1=1 ==> Parameters: 可以看出完全可以使用自定义的方法进行if条件控制。通过该特性可以干一些特俗业务的事情。自己体会。

本篇说的主要是if条件判断动态控制sql。可以看出有弊端。因为if条件不满足的时候sql会变成

select * from t_user where  所以我在条件后面加了个 1=1 但是这是不符合逻辑的。下节介绍where以及其他标签用于动态sql。



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有